<?php
namespace app\common\lib\exception;

use think\exception\Handle;

class ApiHandleException extends Handle{
    public $httpCode = 500;//http状态码

    public $msg_code = '';

    /**
     * api接口返回值
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\Response|\think\response\Json
     */
    public function render(\Exception $e){
        if(config('app_debug') == true){
            //服务端开发调试错误信息
            return parent::render($e);
        }

        //$e是否是实例化的ApiException类
        if($e instanceof ApiException){
            //抛出自定义异常信息
            $this->httpCode = $e->httpCode;
        }

        //抛出未知错误异常信息
        return show(0,$e->getMessage(),[],$this->msg_code,$this->httpCode);
       /* $result = [
            'status' => 0,
            'message' => $e->getMessage(),
            'data' => [],
        ];
        return json($result,$this->httpCode);*/
    }
}